package edu.uoc.tdp.pac3.manager;

import java.util.ArrayList;
import java.util.List;

import edu.uoc.tdp.pac3.beans.Resource;
import edu.uoc.tdp.pac3.beans.ResourceRequest;
import edu.uoc.tdp.pac3.beans.Room;
import edu.uoc.tdp.pac3.database.DataBaseManager;

public class GenericManager {

	public static List<Room> getAllRooms(){
		List<Room> listRoom = new ArrayList<Room>();
		
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		listRoom = gestor.getAllRooms();
		return listRoom;
	}
	
	public static List<Resource> getAllResources(){
		List<Resource> listResources = new ArrayList<Resource>();
		
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		listResources = gestor.getAllResources();
		return listResources;
	}
	
	public static List<ResourceRequest> getAllResourceRequests(){
		List<ResourceRequest> listResourceRequests = new ArrayList<ResourceRequest>();
		
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		listResourceRequests = gestor.getAllResourceRequests();
		return listResourceRequests;
	}
	
	public static List<ResourceRequest> getNoPossibleRequests(){
		List<ResourceRequest> listResourceRequests = new ArrayList<ResourceRequest>();
		
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		listResourceRequests = gestor.getNoPossibleRequests();
		return listResourceRequests;
	}

	public static void addNewStock(String resourceName, int newQuantity) {
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		gestor.addNewStock(resourceName, newQuantity);
		
	}
	
	public static int addResourceRequest(String nomAula, String nomRecurs, int quant){
		int ok = 0;
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		try{
			ok = gestor.addResourceRequest(nomAula, nomRecurs, quant);
		}catch(Exception e){
			ok = -1; //generic error
		}
		return ok;
	}
	
	public static void updateStock(int idRecurs, int quant){
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		gestor.updateStock(idRecurs, quant);
		
	}
	
	public static void finalizeResourceRequest(int quantToDelete, int idResource, int idRoom){
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		gestor.discountStock(idResource, quantToDelete);
		gestor.finalizeRequest(idResource, idRoom);
	}
	
	public static String loadRoomNameById(int roomId){
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		return gestor.loadRoomNameById(roomId);
	}
	
	
	public static String loadResourceNameById(int resourceId){
		DataBaseManager gestor = new DataBaseManager();
		gestor.conectaBD();
		return gestor.loadResourceNameById(resourceId);
	}
}
